﻿using System.Linq;
using System.Web.Mvc;
using CodeContrib.Web.Mvc;
using CodeContrib.Web.Routing;
using CodeContribBlog.Data;

namespace CodeContribBlog.Web.Mvc.Controllers
{
	public class ErrorController : Controller
	{
		public ActionResult InternalServerError()
		{
			var exception = Server.GetLastError();

			return new InternalServerErrorResult(exception);
		}

		public ActionResult NotFound()
		{
			return new NotFoundResult();
		}

		protected override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			base.OnActionExecuted(filterContext);

			if (!HttpContext.Items.Contains(TenantRoute.HttpContextKey))
			{
				return;
			}

			if (HttpContext.Items.Contains(TenantRoute.HttpContextKey))
			{
				using (var blogContext = new BlogContext( /*((TenantRouteInfo)HttpContext.Items[TenantRoute.HttpContextKey]).TenantId*/))
				{
					ViewBag.Blog = blogContext.Blogs.SingleOrDefault();
				}
			}
		}
	}
}